<?php
namespace Destroyer;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class Admin
{
	const VERSION = '1.0.0';

    public static function guard()
    {
        return Auth::guard('admin');
    }

    /**
     * 构建 OAuth2 驱动程序实例 (laravel/socialite)
     * 基本等同于 Socialite::driver($name), 但是配置从admin.php读取，且默认使用无状态认证
     * @link https://github.com/laravel/socialite
     */
    public static function socialite($name)
    {
        // 读取配置
        $config = config('admin.oauth2');
        $providerConfig = Arr::get($config, $name);
        abort_unless($providerConfig, 500, "缺少配置:{$name}");

        // 读取
        $providerClass = Arr::get($providerConfig, 'class', "\Laravel\Socialite\Two\\".Str::studly($name)."Provider") ;
        abort_unless(class_exists($providerClass), 500, "{$name}未配置class，或class不存在{$providerClass}");

        $provider = \Laravel\Socialite\Facades\Socialite::buildProvider($providerClass, $providerConfig);
        return $provider->stateless();// 无状态认证
    }
}